package com.zxs.application.controller;

import com.google.common.base.Preconditions;
import com.zxs.common.entity.Result;
import com.zxs.domain.entity.bo.SubjectCategoryBO;
import com.zxs.domain.entity.dto.SubjectCategoryDTO;
import com.zxs.domain.service.SubjectCategoryDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 刷题模块
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class SubjectController {

    private static final Logger log = LoggerFactory.getLogger(SubjectController.class);
    @Autowired
    private SubjectCategoryDomainService subjectCategoryDomainService;


    /**
     * 新增分类
     *
     * @param subjectCategoryBO
     * @return
     */
    @PostMapping("/addCategory")
    public Result insterCategory(@RequestBody SubjectCategoryBO subjectCategoryBO) {
        Result result = new Result();
        try {

            Preconditions.checkNotNull(subjectCategoryBO.getCategoryType(), "分类类型不能不能为空！");
            Preconditions.checkNotNull(subjectCategoryBO.getParentId(), "分类父级id不能为空！");
            Preconditions.checkArgument(!StringUtils.isBlank(subjectCategoryBO.getCategoryName()), "分类名称不能为空！");

            result = subjectCategoryDomainService.addSubjectCategory(subjectCategoryBO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            result.setSuccess(Boolean.FALSE);
            result.setCode(1);
            result.setMessage(e.getMessage());
            return result;
        }
        return result;
    }

    /**
     * 查询分类
     * @return
     */
    @PostMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(@RequestBody SubjectCategoryBO subjectCategoryBO) {
        Result<List<SubjectCategoryDTO>> result = new Result<>();
        try {
            Preconditions.checkNotNull(subjectCategoryBO.getCategoryType(), "分类类型不能不能为空！");
            List<SubjectCategoryDTO> subjectCategoryDTOList = subjectCategoryDomainService.queryPrimaryCategory(subjectCategoryBO);
            result = Result.success("查询分类成功！", subjectCategoryDTOList);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.failed("服务器异常请联系管理员！");
        }
        return result;
    }

    /**
     * 查询岗位下的小分类
     *
     * @param subjectCategoryBO
     * @return
     */
    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody SubjectCategoryBO subjectCategoryBO) {
        Result<List<SubjectCategoryDTO>> result = new Result<>();
        try {
            Preconditions.checkNotNull(subjectCategoryBO.getCategoryId(), "分类ID不能为空！");
            Preconditions.checkNotNull(subjectCategoryBO.getCategoryType(), "分类类型不能为空！");
            List<SubjectCategoryDTO> subjectCategoryDTOList = subjectCategoryDomainService.querySeconedCategory(subjectCategoryBO);
            result = Result.success("查询二级分类成功！", subjectCategoryDTOList);
            return result;
        }catch (Exception e) {
            log.error("查询岗位下的小分类,报错{}", e.getMessage(), e);
            return Result.failed("服务器异常，请联系管理员！");
        }
    }

    /**
     * 更新分类
     * @param subjectCategoryBO
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryBO subjectCategoryBO) {
        Result<Boolean> result = new Result<>();
        try {
            Preconditions.checkNotNull(subjectCategoryBO.getId(), "分类ID不能为空！");
            Boolean aBoolean = subjectCategoryDomainService.updateCategory(subjectCategoryBO);
            result = Result.success("修改分类成功！！", aBoolean);
            return result;
        }catch (Exception e) {
            log.error("修改分类,报错{}", e.getMessage(), e);
            return Result.failed("服务器异常，请联系管理员！");
        }
    }

    /**
     * 删除分类
     * @param subjectCategoryBO
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> deleteCategory(@RequestBody SubjectCategoryBO subjectCategoryBO) {
        Result<Boolean> result = new Result<>();
        try {
            Preconditions.checkNotNull(subjectCategoryBO.getId(), "分类ID不能为空！");
            Boolean aBoolean = subjectCategoryDomainService.deleteCategory(subjectCategoryBO);
            result = Result.success("删除分类成功！！", aBoolean);
            return result;
        }catch (Exception e) {
            log.error("删除分类,报错{}", e.getMessage(), e);
            return Result.failed("服务器异常，请联系管理员！");
        }
    }



}
